﻿using System;
using System.IO;
using System.IO.Pipes;
using System.Diagnostics;

class PipeServer
{
    private static Boolean playing = true;
    private static string clientResponse = "";

    static void Main()
    {
        Process pipeClient = new Process();
        pipeClient.StartInfo.FileName = "pipeClient.exe";

        AnonymousPipeServerStream pipeServerOut = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable);
        AnonymousPipeServerStream pipeServerIn = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable);

        StreamWriter serverSideStreamWriter = new StreamWriter(pipeServerOut);
        StreamReader serverSideStreamReader = new StreamReader(pipeServerIn);

        using (pipeServerOut)
        {

            // Pass the client process a handle to the server.
            pipeClient.StartInfo.Arguments = pipeServerOut.GetClientHandleAsString() + " " + pipeServerIn.GetClientHandleAsString();
            pipeClient.StartInfo.UseShellExecute = false;
            pipeClient.Start();

            pipeServerOut.DisposeLocalCopyOfClientHandle();

            while(playing)
            {

                try
                    {
                    // Read user input and send that to the client process.
                    using (serverSideStreamWriter)
                    {
                        serverSideStreamWriter.AutoFlush = true;
                        Console.Write("Server says: Do you want to play the rock, scissors, paper game?\n");
                        //serverSideStreamWriter.WriteLine(Console.ReadLine());
                    }
                }
                // Catch the IOException that is raised if the pipe is broken
                // or disconnected.
                catch (IOException e)
                {
                    Console.WriteLine("ERROR: {0}", e.Message);
                }
    
                using (serverSideStreamReader)
                {
                    // Display the read text to the console
                    string temp;
                    while ((temp = serverSideStreamReader.ReadLine()) != null)
                    {
                        clientResponse = temp.ToLower();

                        if(clientResponse =="no")
                        {
                            playing = false;
                        }
                        else if (clientResponse == "yes")
                        {
                            Console.Write("Server says: now choose a word from rock, scissors, paper-->");
                        }
                        //else
                        //{
                            //Console.Write("Server says: Your input was incorrect! Please choose a word from rock, scissors,");
                        //}
                    }  
                }
            }
            






        }

        pipeClient.WaitForExit();
        pipeClient.Close();
    }
}
